#!/usr/bin/python
# Script for automatically run tests on samples
# author: sana
# contributors: tct, alexm

import subprocess
import sys

# list of data structures
tests = ["algorithms",
         "avl_tree",
         "b_tree",
         "binary_search_tree",
         "binary_tree",
         "doubly_linked_list",
         "hash_map",
         "hash_set",
         "heap",
         "interval_tree",
         "linked_list",
         "matrix",
         "graph",
         "graph_matrix",
         "pair",
         "queue",
         "red_black_tree",
         "scapegoat_tree",
         "skip_list",
         "sort",
         "splay_tree",
         "stack",
         "treap_tree",
         "tree_algorithms",
         "tree",
         "trie_tree",
         "vector"]

EXEC = "./temelia_samples "
CROSS_COMPILE = ""

subprocess.call("make clean", shell=True)

for arg in sys.argv:
    if (arg == 'dll'):
        subprocess.call("cp /usr/lib/temelia.dll .", shell=True)
        EXEC = "./temelia_samples.exe "
        CROSS_COMPILE = " CROSS_COMPILE=i586-mingw32msvc-"

subprocess.call("make" + CROSS_COMPILE, shell=True)
LOG_FILE = "samples_log.txt"

flog = open(LOG_FILE, 'w')

# no. of failed and passed tests
failed = 0
passed = 0

print "* Testing samples..."

for test in tests:
    # log each test's output
    flog.write('* Test ' + test + ' begins...\n' )
    flog.write('* Test ' + test + ' ended.\t')
    if (subprocess.call(EXEC + test + " >> " + LOG_FILE, shell=True) != 0):
        print 'FAILED Test [' + test + ']'
        flog.write('[failed]\n')
        failed += 1 
    else:
        print 'PASSED Test [' + test + ']'
        flog.write(' [passed]\n')
        passed += 1 
    
print 'Done.'
print 'Statistics: ' + str(failed) + ' failed tests' + ' | ' + \
str(passed) + ' passed tests.'

print 'See log file ' + LOG_FILE
